package com.czy.basecommon.utils.tools;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;

import android.widget.ImageView;


import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.czy.basecommon.R;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;


public class MyImageLoader {

    public static Bitmap getBitmapFromByte(byte[] temp){
        if(temp != null){
            Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length);
            return bitmap;
        }else{
            return null;
        }
    }
    public static byte[] getBitmapByte(Bitmap bitmap){
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        try {
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }
    public static void loadSpn(Context context, String path, ImageView v,int errRes) {
        RequestOptions options = new RequestOptions()
                .placeholder(errRes)
                .error(errRes);
        Glide.with(context)
                .load(path)
                .transition(DrawableTransitionOptions.withCrossFade())
                .apply(options)
                .into(v);
    }

    public static void print(Context context,String path, ImageView v,int errRes) {
        RequestOptions options = new RequestOptions()
                .placeholder(errRes)
                .error(errRes)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(context)
                .asBitmap().load("file:///" + path)
                .apply(options)
                .into(v);

    }

    public static void load(Context context,String path, ImageView v,int errRes) {
        RequestOptions options = new RequestOptions()
                .placeholder(errRes)
                .error(errRes)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(context).asBitmap().load(path)
                .apply(options)
                .into(v);
    }






//    public static void loads(Context context,String path, ImageView v,int errRes) {
//        RequestOptions options = new RequestOptions()
//                .placeholder(errRes)
//                .error(errRes)
//                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
//        Glide.with(context).asBitmap().load(path)
//                .apply(options)
//                .into(v);
//
//    }
    public static void load(Context context,String path, ImageView v, int res,int errRes) {
        RequestOptions options = new RequestOptions()
                .placeholder(res)
                .error(errRes)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(context).asBitmap().load(path)
                .apply(options)
                .into(v);

    }


    public static void local(Context context,int res, ImageView v,int errRes) {
        RequestOptions options = new RequestOptions()
                .placeholder(res)
                .error(errRes)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(context).asBitmap().load(res)
                .apply(options)
                .into(v);
    }



    /**
     * @author zjq
     * create at 2018/12/26 上午9:45
     * 添加加载成功的回调
     */
    //注释掉，此处加载C代码速度过慢，使用的时候咨询一下  再优化
   /* public static void loadListeren(final Context context, String path, final ImageView v, final boolean b, final String name) {
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with( context).asBitmap().load(path)
                .apply(options)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        if (b) {
                            WatermarkText watermarkText = new WatermarkText(name)
                                    .setTextColor(Color.WHITE)
                                    .setPositionX(0.5)
                                    .setTextAlpha(200)
                                    .setTextSize(11f)
                                    .setPositionY(0.8);
                            //需要添加水印
                            WatermarkBuilder
                                     .create(context,resource)
                                     .loadWatermarkText(watermarkText)
                                     .getWatermark()
                                     .setToImageView(v);


                        } else {
                            v.setImageBitmap(resource);
                        }
                    }
                });
    }*/


   //自定义错误图

    public static void loads(Context context,String path, ImageView v,int error) {
        RequestOptions options = new RequestOptions()
                .placeholder(error)
                .error(error)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(context).asBitmap().load(path)
                .apply(options)
                .into(v);
    }


    }
